package com.tedu;

import com.tedu.ioc.Bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

//手写ioc容器
public class MyIoc {
    List<Bean> list = new ArrayList<>();
    ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
    //2初始化bean工厂
    public MyIoc() throws Exception {
        Bean b1 = new Bean("user", "com.tedu.ioc.user");
//        Bean b2 = new Bean("user", "com.tedu.ioc.dept");
        Collections.addAll(list, b1);

        createObj();
    }
    //3进行IOC，核心就是map里的put(bean的name属性的值，根据bean的path值创建的对象)
    private void createObj() throws Exception {
        for (Bean b : list) {
            String k = b.getName();
            Object v = Class.forName(b.getPath()).newInstance();
            map.put(k,v);
        }
    }
    //4getbean返回创建好的对象
    public Object  getbean(String name){
        return map.get(name);
    }
}
